//Page Object
const url = require('../../config/config.js')
    // const Encrypt = require('../../config/jsencrypt.min.js')
Page({
    data: {

    },
    userVo: {
        acount: "",
        password: 0
    },
    // 勾选
    agree: [],
    // 公钥
    publicKey: null,
    // 用户类型
    user: {
        type: 0, // type用来判断用户
        adminType: 0 //adminType用来判断管理员用户类型
    },
    // 用户名
    userName: "",
    // 密码
    password: "",
    // 判断示部是登陆过期
    onShow() {
        // 页面一开始进入时候都会加载公钥
        // this.getpublicKey();

    },
    // 获取用户名
    handleUser(e) {
        this.userName = e.detail.value;
        // let encryptor = new Encrypt.JSEncrypt();
        // encryptor.setPublicKey(this.publicKey) // 设置公钥
        // this.userVo.acount = encryptor.encrypt(e.detail.value);
        this.userVo.acount = e.detail.value;

    },
    // 获取用户密码
    handlePass(e) {
        this.password = e.detail.value;
        // let encryptor = new Encrypt.JSEncrypt();
        // encryptor.setPublicKey(this.publicKey) // 设置公钥
        // this.userVo.password = encryptor.encrypt(e.detail.value);
        this.userVo.password = e.detail.value;

    },
    handleLoginOne() {
        // 发送数据请求
        wx.request({
            url: url.b.login,
            data: { userVo: this.userVo },
            header: { 'content-type': 'application/json' },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result) => {
                console.log(result)
                const { code } = result.data
                if (code === 1) {
                    const { token } = result.data.data;
                    var tokentime = Date.parse(new Date())
                    console.log(tokentime)
                    wx.setStorageSync("tokentime", tokentime);
                    wx.setStorageSync("token", token);
                    this.getUserInfo();
                    // 判断有没有授权成功
                    // 取出本地缓存user
                    const user = wx.getStorageSync("user");
                    if (user) {
                        wx.switchTab({
                            url: '/pages/index/index',

                        });
                    } else {
                        wx.navigateTo({
                            url: '/pages/auth/index',
                            fail(err) {
                                console.log(err)
                            }
                        });
                    }

                } else {
                    wx.showToast({
                        title: '请检查用户名或者密码是否正确',
                        icon: 'none'

                    });
                }
            },
            fail: (err) => {
                wx.showToast({
                    title: '请检查网络是否正常',
                    icon: 'none',
                });
                console.log(err)
            }
        });

    },
    //获取用户信息
    getUserInfo() {
        const header = {
            'content-type': 'application/json',
            'Cookie': "JSESSIONID=" + wx.getStorageSync("token") //读取cookie
        }
        wx.request({
            url: url.b.user,
            data: {},
            header: header,
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result) => {
                console.log('用户信息')
                console.log(result)
                console.log('-------------')
                const { id } = result.data.data.data;
                this.user.type = result.data.data.data.type;
                this.user.adminType = result.data.data.data.adminType;
                this.user.userName = result.data.data.data.userName;
                console.log(this.user.type, this.user.adminType)
                wx.setStorageSync("user", this.user);
                wx.setStorageSync("id", id);


            }
        });
    },
    // 获取公钥
    getpublicKey() {
        wx.request({
            url: url.b.publicKey,
            data: {},
            header: { 'content-type': 'application/json' },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (res) => {
                this.publicKey = res.data.data.publicKey
                console.log(this.publicKey)

            }

        });
    },
    // 用户勾选
    bindAgreeChange(e) {
        this.agree = e.detail.value;
        console.log(this.agree.length)
    },
    // 判断有没有勾选协议和输入密码
    handleLogin() {
        console.log(this.agree.length, this.userName.length, this.password.length)
        if (this.agree.length > 0 && this.userName.length > 0 && this.password.length > 0) {
            // 发起登录请求
            this.handleLoginOne();
        } else {
            wx.showToast({
                title: '请同意协议和输入账号和密码',
                icon: 'none',

            });
        }
    }

});